package ${pack}.${module}.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.admin.common.enums.ResultEnum;
import com.syyo.admin.common.exception.SysException;
import ${pack}.${module}.domain.req.${className}Req;
import ${pack}.${module}.domain.entity.${className}Entity;
import ${pack}.${module}.domain.export.${className}Export;
import ${pack}.${module}.mapper.${className}Mapper;
import ${pack}.${module}.service.${className}Service;

import com.syyo.admin.utils.MyFileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: ${author}
 * @date: ${date}
 */
@Service
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}Entity> implements ${className}Service {

    @Autowired
    private ${className}Mapper ${lowerClassName}Mapper;

    /**
     * 新增
     */
    @Override
    @Transactional
    public int add(${className}Req req){
        ${className}Entity entity = new ${className}Entity();
        // 添加创建时间和更新时间
        BeanUtils.copyProperties(req,entity);
        LocalDateTime now = LocalDateTime.now();
<#list  fields as field>
    <#if field.fieldNameDb == "create_time" >
        entity.setCreateTime(now);
    </#if>
    <#if field.fieldNameDb == "update_time" >
        entity.setCreateTime(now);
    </#if>
</#list>
        int insert = ${lowerClassName}Mapper.insert(entity);
        if (insert != 1){
            throw new SysException(ResultEnum.E_10001.getCode(),ResultEnum.E_10001.getMessage());
        }
        return insert;
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public int del(${className}Req req){
        List<Integer> ids = req.getIds();
        boolean b = removeByIds(ids);
        if (!b) {
            throw new SysException(ResultEnum.E_10002.getCode(), ResultEnum.E_10002.getMessage());
        }
        return 1;
    }

    /**
     * 编辑
     */
    @Override
    @Transactional
    public int edit(${className}Req req){
        ${className}Entity entity = new ${className}Entity();
        BeanUtils.copyProperties(req,entity);
        // 添加更新时间
<#list  fields as field>
    <#if field.fieldNameDb == "update_time" >
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
    </#if>
</#list>
        int insert = ${lowerClassName}Mapper.updateById(entity);
        if (insert != 1){
            throw new SysException(ResultEnum.E_10003.getCode(),ResultEnum.E_10003.getMessage());
        }
        return insert;
    }

    /**
     * 详情
     */
    @Override
    public ${className}Entity findOne(Integer id){
        return ${lowerClassName}Mapper.selectById(id);
    }

    /**
     * 列表
     */
    @Override
    public IPage<${className}Entity> findPage(Integer pageNum, Integer pageSize, ${className}Req req){
        Page<${className}Entity> teacherPage = new Page<${className}Entity>(pageNum,pageSize);
        QueryWrapper<${className}Entity> wrapper = new QueryWrapper<>();
        return ${lowerClassName}Mapper.selectPage(teacherPage, wrapper);
    }

    /**
     * 导出
     */
    @Override
    public void export(HttpServletResponse response) {
        List<${className}Entity> entitys = ${lowerClassName}Mapper.selectList(null);
        List<${className}Export> list = new ArrayList<>();
        for (${className}Entity entity : entitys) {
            ${className}Export export = new ${className}Export();
            BeanUtils.copyProperties(entity, export);
            list.add(export);
        }
        // 导出文件
        MyFileUtil.exportFile(response, "${apiName}", list, ${className}Export.class);
    }

}